<template>
  <div class="main-center">
    <el-card class="position-edit-card">
      <h2>{{ isEdit ? '编辑岗位' : '发布新岗位' }}</h2>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="岗位名称" prop="positionName">
          <el-input v-model="form.positionName" />
        </el-form-item>
        <el-form-item label="岗位职责" prop="responsibility">
          <el-input v-model="form.responsibility" type="textarea" rows="3" />
        </el-form-item>
        <el-form-item label="任职要求" prop="requirement">
          <el-input v-model="form.requirement" type="textarea" rows="3" />
        </el-form-item>
        <el-form-item label="薪资范围" prop="salaryRange">
          <el-input v-model="form.salaryRange" placeholder="如 8k-12k/月" />
        </el-form-item>
        <el-form-item label="招聘人数" prop="recruitmentNum">
          <el-input-number v-model="form.recruitmentNum" :min="1" />
        </el-form-item>
        <el-form-item label="工作地点" prop="workLocation">
          <el-input v-model="form.workLocation" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="loading">{{ isEdit ? '保存修改' : '发布岗位' }}</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from '@/utils/axios'

const route = useRoute()
const router = useRouter()
const isEdit = ref(false)
const form = ref({
  positionName: '', responsibility: '', requirement: '', salaryRange: '', recruitmentNum: 1, workLocation: ''
})
const rules = {
  positionName: [{ required: true, message: '请输入岗位名称', trigger: 'blur' }],
  responsibility: [{ required: true, message: '请输入岗位职责', trigger: 'blur' }],
  requirement: [{ required: true, message: '请输入任职要求', trigger: 'blur' }],
  salaryRange: [{ required: true, message: '请输入薪资范围', trigger: 'blur' }],
  recruitmentNum: [{ required: true, message: '请输入招聘人数', trigger: 'blur' }],
  workLocation: [{ required: true, message: '请输入工作地点', trigger: 'blur' }]
}
const formRef = ref()
const loading = ref(false)

const fetchDetail = async () => {
  if (route.params.id) {
    isEdit.value = true
    try {
      const res = await axios.get(`/position/${route.params.id}`)
      form.value = { ...res }
    } catch (e) {}
  }
}
const onSubmit = () => {
  formRef.value.validate(async valid => {
    if (!valid) return
    loading.value = true
    try {
      if (isEdit.value) {
        await axios.put(`/position/${route.params.id}`, form.value)
        ElMessage.success('岗位修改成功')
      } else {
        await axios.post('/position/publish', form.value)
        ElMessage.success('岗位发布成功')
      }
      router.push('/enterprise/positions')
    } catch (e) {}
    loading.value = false
  })
}
const goBack = () => router.push('/enterprise/positions')
onMounted(() => {
  fetchDetail()
})
</script>

<style scoped>
.position-edit-card {
  max-width: 600px;
  margin: 40px auto;
  padding: 30px 20px;
}
</style> 